//
//  ShareView.m
//  BDShareDemo
//
//  Created by Faker on 6/3/14.
//  Copyright (c) 2014 Faker. All rights reserved.
//

#import "ShareView.h"
#import "Header.h"
#import "UIView+EidanSize.h"
//#import "AppDelegate.h"
#import <Frontia/Frontia.h>
#import "WXApi.h"
#import "QQApi.h"
@interface ShareView()


@property (strong, nonatomic) UIView* maskView;
@property (strong, nonatomic) IBOutlet UIView *customView;
@property (assign, nonatomic) BOOL isShow;

@property (strong, nonatomic) NSString* url;
@property (strong, nonatomic) NSString* title;
@property (strong, nonatomic) NSString* info;
@property (strong, nonatomic) NSString* imageUrl;

@property (strong, nonatomic) Completion isSuccess;

@property (weak, nonatomic) IBOutlet UIButton *cancelBtn;


@end

@implementation ShareView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        //[self show];
        
    }
    return self;
}

-(void)showWithUrl:(NSString *)url andWithTitle:(NSString *)title andWithInfo:(NSString *)info andWithImageUrl:(NSString *)imageUrl andSuccess:(Completion)successBlock
{
    
    FrontiaShare *share = [Frontia getShare];
    [share registerSinaweiboAppId:SinaAppKey];
    [share registerWeixinAppId:WXAppID];
    [share registerQQAppId:QQAppKey enableSSO:YES];
    
    self.url = url;
    self.isSuccess = successBlock;
    self.title = title;
    self.info = info;
    self.imageUrl = imageUrl;
    self.maskView = [[UIView alloc]initWithFrame:self.bounds];
    self.maskView.backgroundColor = [UIColor blackColor];
    self.maskView.alpha = 0;
    [self addSubview:self.maskView];
    
    self.customView = [[[NSBundle mainBundle] loadNibNamed:@"ShareView" owner:self options:nil] lastObject];
    self.cancelBtn.layer.cornerRadius = 5;
    self.cancelBtn.clipsToBounds = YES;
    self.customView.top = self.height;
    [self addSubview:self.customView];
    
    [UIView animateWithDuration:0.3 animations:^{
    
        self.customView.top -=self.customView.height;
        self.maskView.alpha = 0.5;
    } completion:^(BOOL finished) {
        UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideView:)];
        [self.maskView addGestureRecognizer:tap];
        
    }];
    
}

-(void)hideView:(UITapGestureRecognizer*)tap
{
    
    [UIView animateWithDuration:0.3 animations:^{
        self.customView.top = self.height;
        self.maskView.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
      
    }];
}

- (IBAction)btnClick:(id)sender {
    UIButton* btn = (UIButton*)sender;
    int tag = (int)btn.tag;
    if(tag == 101)
    {
       [self shareToPlatName:FRONTIA_SOCIAL_SHARE_PLATFORM_SINAWEIBO];
        
    }
    if(tag == 102)
    {
        if(![WXApi isWXAppInstalled])
        {
            [PopTips tips:kApp.window Msg:NotifyNoWX];
        }
        
        else{
            self.url = ShareDefaultUrl;
        [self shareToPlatName:FRONTIA_SOCIAL_SHARE_PLATFORM_WEIXIN_SESSION];
        }
    }
    if(tag == 103)
    {
        if(![WXApi isWXAppInstalled])
        {
            [PopTips tips:kApp.window Msg:NotifyNoWX];
        }
        else{
            self.url = ShareDefaultUrl;
        [self shareToPlatName:FRONTIA_SOCIAL_SHARE_PLATFORM_WEIXIN_TIMELINE];
        }
    }
    if(tag == 104)
    {
        if(![QQApi isQQInstalled])
        {
            [PopTips tips:kApp.window Msg:NotifyNoQQ];
        }
        else{
        [self shareToPlatName:FRONTIA_SOCIAL_SHARE_PLATFORM_QQFRIEND];
        }
        
    }
    if(tag == 105)
    {
        if(![QQApi isQQInstalled])
        {
            [PopTips tips:kApp.window Msg:NotifyNoQQ];
        }
        else{
        [self shareToPlatName:FRONTIA_SOCIAL_SHARE_PLATFORM_QQ];
        }
    }
    if(tag == 106)
    {
        [self hideView:nil];
    }
}

-(void)shareToPlatName:(NSString*)platName
{
    
    
    FrontiaShareContent *content = [[FrontiaShareContent alloc] init];
    content.url = self.url;//必须，分享的链接
    content.description = self.info;//必须，分享的描述
    content.title = self.title;//必须，分享的标题
    [content addImageWithThumbImage:nil imageUrl:self.imageUrl];
   
    
    [[Frontia getShare] shareWithPlatform:platName content:content supportedInterfaceOrientations:UIInterfaceOrientationMaskAllButUpsideDown isStatusBarHidden:NO cancelListener:^{
           
        [self hideView:nil];
        } failureListener:^(int errorCode, NSString *errorMessage) {
            self.isSuccess(NO);
            [self hideView:nil];
        } resultListener:^{
            [self hideView:nil];
            self.isSuccess(YES);
        }];
    
}



@end
